package com.example.homework2.controller;
import com.example.homework2.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller

public class CalculateController {
    @RequestMapping("/calculate")
    public String calculate(@RequestParam(value = "firstNum", required = false) String firstNum,
                            @RequestParam(value = "secondNum", required = false) String secondNum,
                            @RequestParam(value = "operator", required = false) String operator,
                            Model model) {
        try {
            double firstnum = Double.parseDouble(firstNum);
            double secondnum = Double.parseDouble(secondNum);
            double result = 0;
            switch (operator) {
                case "+":
                    result = firstnum + secondnum;
                    break;
                case "-":
                    result = firstnum - secondnum;
                    break;
                case "*":
                    result = firstnum * secondnum;
                    break;
                case "/":
                    result = firstnum / secondnum;
                    break;
                default:
                    throw new Exception("运 算 符 错 误");
            }
            model.addAttribute("result", result);
        } catch (Exception e) {
            model.addAttribute("result", e.getMessage());
        }
       return "calculate";
    }
}
